<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-friendadd"></i>媒体账号维护
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" @click="ALL" class="lkx_r20">全部</el-button>
        <el-input
          v-model.trim="select_word"
          placeholder="筛选账号名称关键词"
          class="handle-input mr10"
          @keyup.enter.native="search_key(select_word)"
        ></el-input>
        <el-button type="primary" icon="search" @click="search_key(select_word)">搜索</el-button>
        <span class="zh_status">账号状态选择:</span>
        <el-select
          v-model="status_value"
          placeholder="请选择"
          class="status_select"
          @change="select_status"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button type="primary" icon="search" @click="select_status">查看</el-button>
      </div>
      <el-table
        border
        ref="singleTable"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
        <el-table-column prop="user_name" label="账号名称" align="center"></el-table-column>
        <el-table-column prop="type" label="账号类型" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" align="center"></el-table-column>
        <el-table-column prop="attention_today" label="今日关注数量 " align="center"></el-table-column>
        <el-table-column prop="attention_num" label="关注总人数 " align="center"></el-table-column>
        <el-table-column label="创建时间"  width="200" align="center">
          <template slot-scope="scope">{{ scope.row.add_time }}</template>
        </el-table-column>
        <el-table-column label="申请时间"  width="200" align="center">
          <template slot-scope="scope">{{ scope.row.apply_time }}</template>
        </el-table-column>
        <el-table-column label="文章是否审核" width="120" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_review"
              :disabled="scope.row.TFs"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="switchs(scope.$index, scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="review_admin_name" label="审核人员" align="center"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.TFs"
              :type="scope.row.CZtype"
              @click="operation(scope.$index, scope.row)"
            >{{ scope.row.operation }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="查看详情" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination_div">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="Totals"
          :page-count="pageTotal"
          :page-size="10"
          :current-page.sync="currPage"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import * as api from "../../http/api.js";

export default {
  data() {
    return {
      //表格数据
      tableData: [],
      options: [
        {
          value: "0",
          label: "全部"
        },
        {
          value: "2",
          label: "已驳回"
        },
        {
          value: "1",
          label: "正常"
        },
        {
          value: "3",
          label: "禁用"
        }
      ],
      status_value: "", //默认状态下拉
      select_word: "", //搜索的关键词
      editVisible: false, //查看详情开关
      Token: sessionStorage.getItem("Token"),
      currPage: 1, //分页页码
      Totals: 1, //总条数 默认为1条，在获取接口列表时在传递过来，改变1
      pageTotal: 2, //总页数
      Media_id: "", //媒体id 用来连接详情的参数
      typenumber: "",//操作按钮对应的数字
      artBR:false// swithc按钮 代表的数字.
    };
  },
  methods: {
    getData(statuszt,key) {
      //获得表格数据！
      this.$post(api.media_list, {
        token: this.Token, //用户token
        user_name:key, //账号名称，检索条件，非必填
        status: statuszt, //账号名状态，检索条件，非必填 0全部 1正常 2驳回 3禁用
        page: this.currPage //页数 初始值为1
      })
        .then(res => {
          if (res.status == "0") {
            this.tableData = res.data.map((val, index, arrr) => {
              let jsons = [];
              jsons.user_name = val.user_name;
              jsons.type = val.type;              
              jsons.review_admin_name=val.review_admin_name;
              jsons.add_time = val.add_time;
              jsons.apply_time = val.apply_time;
              jsons.attention_num=val.attention_num;
              jsons.attention_today=val.attention_today;

              jsons.media_id = val.media_id; //媒体id
              jsons.status = val.status; //状态
              jsons.is_review=val.is_review//发布文章是否需要审核。type ：1 需要审核 2 不需要审核
              jsons.operation = val.operation; //操作里面按钮的文字
              jsons.CZtype = val.CZtype; //操作 案例中控制颜色
              jsons.typenumber = val.typenumber; // 操作所对应的数字含义！
              jsons.TFs = val.TFs; //判断按钮是否禁用
              return jsons;
            });
            this.tableData.forEach((val, index, arr) => {
              if (val.type == 1) {
                val.type = "个人";
              } else {
                val.type = "组织";
              }

              if (val.status == 1) {
                val.status = "正常"; //改变按钮颜色和文字 同时把对应的状态转成对应的数字！ 3禁用 4恢复禁用账号
                arr[index].operation = "禁用";
                arr[index].typenumber = 3;
                arr[index].CZtype = "danger";                            
              }
              if (val.status == 2) {
                val.status = "驳回";
                arr[index].operation = "请查看详情";
                arr[index].TFs = true;
              }
              if (val.status == 3) {
                val.status = "禁用";
                arr[index].operation = "启动";
                arr[index].typenumber = 4;
                arr[index].CZtype = "primary";
              }
              
              // 判断文章是否需要审核：
              if(val.is_review=="1"){//1 需要审核 2 不需要审核
                val.is_review=true;
              }
              else{
                val.is_review=false;
              }

              if(val.is_review=="0"){
                  val.review_admin_name="免审账号"
              }

            });
            // 获取分页数据
            this.Totals = res.total;
            this.pageTotal = res.pageTotal;
            this.currPage = res.currPage;
          } else {
            console.log(res.msg);
          }
          // 如果登录过期
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    ALL() {
      //展示全部！状态 传值参数 为0、
      this.currPage = 1;
      this.select_word = "";
      this.status_value = "";
      this.getData(this.status_value,this.select_word);
    },
    //搜索关键词
    search_key(select_word) {
      if (select_word == "") {
        this.$message.error(`搜索不能为空`);
        return;
      } else {
        this.currPage = 1;
        this.getData(this.status_value,this.select_word);
      }
    },
    //查看详情页面
    handleEdit(index, row) {
      //类型：个人就跳转到个人页面，企业就跳转企业
      this.Media_id = this.tableData[index].media_id;
      if (row.type == "个人") {
        this.$router.push({
          path: "/personInfoLook",
          query: { id: this.Media_id }
        });
      } else {
        this.$router.push({
          path: "/enterpriseInfoLook",
          query: { id: this.Media_id }
        });
      }
    },
    select_status() {
      //下拉列表： 选择状态！
    this.currPage = 1;
    this.getData(this.status_value,this.select_word);
    },
    //点击分页
    handleCurrentChange(val) {
      this.currPage = val;
      this.getData(this.status_value,this.select_word);
    },
    operation(index, row) {
      //禁用或启动按钮！
      this.Media_id = this.tableData[index].media_id; //媒体id
      this.typenumber = this.tableData[index].typenumber; //操作对应的数字
      this.$post(api.media_check, {
        token: this.Token,
        media_id: this.Media_id,
        type: this.typenumber
      })
        .then(res => {
          if (res.status == "0") {
            this.$message({
              type: "success",
              message: "修改成功！"
            });
            let that = this;
            setTimeout(function() {
              that.getData(this.status_value.this.select_word);
            }, 1200);
          } else {
            this.$message.error(res.msg);
          }
          // 如果登录过期
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    switchs(index, row) {
      //文章是否需要审核：
      this.Media_id = this.tableData[index].media_id; //广告id
      this.artBR=this.tableData[index].is_review; //操作  是 否
      // 因为上面获取到的是 true 或者false 这里需要在此转换一下
      if(this.artBR==true){
        this.artBR=1;
      }
      else{
        this.artBR=2;
      }
      this.$post(api.media_is_review, {
        token: this.Token,
        type: this.artBR, // type 1 需要审核 2 不需要审核
        media_id: this.Media_id
      })
        .then(res => {
          if (res.status == "0") {
            this.$message({
              type: "success",
              message: "修改成功！"
            });
            setTimeout(() => {
             this.getData(this.status_value,this.select_word);
            },1000);
          } else {
            this.$message.error(res.msg);
          }
          // 如果登录过期
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  },
  created() {
    this.getData(this.status_value,this.select_word);
  }
};
</script>
<style scoped>
.mr10 {
  margin-right: 10px;
}
.handle-input {
  width: 300px;
  display: inline-block;
}
.handle-box {
  padding-bottom: 20px;
}
.zh_status {
  line-height: 32px;
  margin-left: 100px;
}
.status_select {
  margin: 0 10px;
}
.pagination_div {
  margin-top: 20px;
  text-align: right;
}
</style>